var Sequelize = require('sequelize');

var auth = require('../../helpers/auth.js');
var db = require('../../models/index.js');
var render = require('../../instances/render.js');
var debug = require('../../instances/debug.js');
var request = require('co-request');
var GoodsType = db.models.GoodsType;

module.exports = (router) => {

    router.get('/user/index/:shopid',  function *() {
        this.checkParams('shopid').isInt().toInt();
        if(this.errors){
            this.body = this.errors;
            return;
        }
        var types = yield GoodsType.findAll({
            where: {
                type: 1,
                shopId:this.params.shopid
            },
            attributes: ['title', 'type', 'id'],
            include: [GoodsType]
        });

        this.body = yield render('phone/index.html', {
            types: JSON.stringify(types)
        });
    });

    router.get('/user/center',  function *() {

        this.body = yield render('phone/user.html', {
            user: yield auth.user(this),
            title: '个人中心'
        });
    });

    router.get('/user/home',function *(){
        var ads = yield db.models.ads.findAll({});
        var imgs = [];
        for(var x in ads){
            imgs.push(ads[x].imgUrl);
        }
        imgs = JSON.stringify(imgs);
        this.body = yield render('phone/home.html',{
            title: '首页',
            data:{
                imgs:imgs
            }
        });
    });

    //前期商店较少先全部显示
    //后期做大后直接首页直接定位得到一级区域下所有商店
    router.get('/user/shopData',function *(){
        //var ip = this.request.ip.substr(7);
        //if(ip === '127.0.0.1'){
        //    ip = '210.30.107.8';
        //}
        //var url = 'http://api.map.baidu.com/location/ip?ak=cANwreG2yxaCKCkFnkTbpKDO&ip=' + ip;
        //var location = '';
        //var  data =yield request.get(url);
        //data = JSON.parse(data.body);
        //city = data.content.address_detail.city;
        var data =  yield db.models.shops.findAll({
            where:{
                status:1
            },
            include:[{
                model:db.models.Container,
                attributes:['value']
            },{
                model:db.models.Area,
                attributes:['title']
            }]
        });
       this.body =  data;
    });

    router.get('/user/location',function *(){
        var ip = this.request.ip.substr(7);
        if(ip === '127.0.0.1'){
            ip = '210.30.107.8';
        }
        var url = 'http://api.map.baidu.com/location/ip?ak=cANwreG2yxaCKCkFnkTbpKDO&ip=' + ip;
        var location = '';
        var  data =yield request.get(url);
        data = JSON.parse(data.body);
        this.body = data.content.address_detail.city;
    });
    //router.get('/all-ltype', function *() {
    //    var types = yield GoodsType.allLtype();
    //    this.body = types;
    //});

    router.get('/user/search/shops',function *(){
        var req_string = this.request.querystring;
        if(!req_string){
            return;
        }
        req_string = decodeURI(req_string);
        //根据商店名的搜索结果：
        var shopNameResult = yield db.models.shops.findAll({
            where:{
                title:{
                    $like:'%' + req_string + '%'
                }
            },
            include:[{
                model:db.models.Container,
                attributes:['value']
            },{
                model:db.models.Area,
                attributes:['title']
            }]
        });
        //根据二级区域的搜索结果：
        var shopLocationResult = yield db.models.Area.findAll({
            where:{
                title:{
                    $like:'%' + req_string + '%'
                },
                type:2
            },
            attributes:['id'],
            include:[{
                model:db.models.shops,
                include:[{
                    model:db.models.Container,
                    attributes:['value']
                },{
                    model:db.models.Area,
                    attributes:['title']
                }]
            }]
        });
        shopLocationResult = shopLocationResult.map(function(item){
            return item.shops[0];
        });
        this.body = shopNameResult.concat(shopLocationResult);

    });

};